package Android.Billarm;

import android.content.res.Resources;
import android.graphics.Canvas;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Paint;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.view.View;
import android.widget.ImageView;
import android.util.Log;

import java.util.List;

public class BillIcon
{
    public static final int RED   = 0;
    public static final int GREEN = 1;
    public static final int BLUE  = 2;
    public static final int GREY  = 3;

    public static int ncolors () { return n_unique_colors; }
    public static int nicons () { return n_unique_icons; }

    /* Convert the icon number and color to a key */
    public static int iconKey (int i, int color)
    {
        return i*ncolors() + color;
    }

    public static void setImageView (int i, int color, ImageView im)
    {
        setImageView (iconKey(i, color), im);
    }

    /* Don't pass a null here */
    public static void setImageView (int key, ImageView im)
    {
        im.setImageResource (icons[key]);
    }

    public static void setPlainColor (int color, ImageView im)
    {
        im.setImageResource (plain_icons[color]);
    }

    /* Concatenate bill icons together to form a Drawable */
    public static Drawable catIcons (List<Integer> keys, int full_width, Resources resource)
    {
        int spacing = 0;  /* Spacing between icons */
        int padding = 2;  /* Padding on sides */
        int full_height = 60 + padding*2;  /* Height of bitmap */

        Bitmap bmaps[] = new Bitmap[keys.size()];

        Paint fillTheme = new Paint ();
        fillTheme.setColor (0xAAFFFFFF);

        int width = 0;

        for (int i = 0; i < bmaps.length; ++i)
        {
            bmaps[i] = BitmapFactory.decodeResource (resource, icons[keys.get(i)]);
            if (full_height < bmaps[i].getHeight ())
                full_height = bmaps[i].getHeight ();
            width += bmaps[i].getWidth();
        }

        Bitmap result_bmap = Bitmap.createBitmap (full_width, full_height, Bitmap.Config.RGB_565);
        Canvas canvas = new Canvas (result_bmap);
        canvas.drawPaint (fillTheme);

        int x = full_width - padding + spacing;

        for (int i = 0; i < bmaps.length; ++i)
        {
            x -= bmaps[i].getWidth() + spacing;
            canvas.drawBitmap (bmaps[i], x, padding, fillTheme);
        }

        return new BitmapDrawable (result_bmap);
    }

    private static final int[] plain_icons =
    { R.drawable.template_r, R.drawable.template_g, R.drawable.template_b, R.drawable.template_gr };

    private static final int[] icons =
    {
        R.drawable.car_r,       R.drawable.car_g,       R.drawable.car_b,       R.drawable.car_gr,      
        R.drawable.computer_r,  R.drawable.computer_g,  R.drawable.computer_b,  R.drawable.computer_gr, 
        R.drawable.credit_r,    R.drawable.credit_g,    R.drawable.credit_b,    R.drawable.credit_gr,   
        R.drawable.health_r,    R.drawable.health_g,    R.drawable.health_b,    R.drawable.health_gr,   
        R.drawable.house_r,     R.drawable.house_g,     R.drawable.house_b,     R.drawable.house_gr,    
        R.drawable.lightning_r, R.drawable.lightning_g, R.drawable.lightning_b, R.drawable.lightning_gr,
        R.drawable.mmo_r,       R.drawable.mmo_g,       R.drawable.mmo_b,       R.drawable.mmo_gr,      
        R.drawable.music_r,     R.drawable.music_g,     R.drawable.music_b,     R.drawable.music_gr,    
        R.drawable.newspaper_r, R.drawable.newspaper_g, R.drawable.newspaper_b, R.drawable.newspaper_gr,
        R.drawable.phone_r,     R.drawable.phone_g,     R.drawable.phone_b,     R.drawable.phone_gr,    
        R.drawable.trash_r,     R.drawable.trash_g,     R.drawable.trash_b,     R.drawable.trash_gr,    
        R.drawable.water_r,     R.drawable.water_g,     R.drawable.water_b,     R.drawable.water_gr,    
        R.drawable.wifi_r,      R.drawable.wifi_g,      R.drawable.wifi_b,      R.drawable.wifi_gr,     
        R.drawable.work_r,      R.drawable.work_g,      R.drawable.work_b,      R.drawable.work_gr     
    };

    private static final int n_unique_colors = GREY+1;
    private static final int n_unique_icons = icons.length / ncolors ();
}

